﻿using FyCaches.Core.Services;
using FyCaches.Models;
using FySystem.Extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FyCaches.Core.CommandHandlers
{
    internal class SetDataCommand
    {
        /// <summary>
        /// 设置数据
        /// </summary>
        /// <param name="command"></param>
        /// <exception cref="NotImplementedException"></exception>
        internal static int Handle(CommandInfo command)
        {
            int res = -1;
            try
            {
                //解析参数部分，参数从index[40]开始
                ReadOnlySpan<byte> span = command.Frame.Frame.AsSpan();

                //2字节key长度
                ushort keyLength = BitConverter.ToUInt16(span.Slice(40, 2));

                //key
                string key = Encoding.UTF8.GetString(span.Slice(42, keyLength));

                var cache = CacheFlowService.Caches[command.DbIndex];
                if (cache.ContainsKey(key))
                {
                    cache[key] = span.Slice(keyLength + 42).ToArray();
                    res = 1;
                }
                else
                {
                    cache.Add(key, span.Slice(keyLength+42).ToArray());
                    res = 0;
                }
            }
            catch (Exception)
            {
                res = -1;
                throw;
            }
            return res;
        }

        /// <summary>
        /// 批量设置值
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        internal static int BatchHandle(CommandInfo command)
        {
            int res = 0;
            try
            {
                ushort keyLength = 0;
                string key = string.Empty;
                int valLength = 0;
                int index = 40;

                ReadOnlySpan<byte> span = command.Frame.Frame.AsSpan();
                while (true)
                {
                    //key
                    keyLength = BitConverter.ToUInt16(span.Slice(index, 2));
                    index += 2;
                    key = Encoding.UTF8.GetString(span.Slice(index, keyLength));
                    index += keyLength;

                    //数据
                    valLength = BitConverter.ToInt32(span.Slice(index, 4));
                    index += 4;

                    var cache = CacheFlowService.Caches[command.DbIndex];
                    if (cache.ContainsKey(key))
                    {
                        cache[key] = span.Slice(index, valLength).ToArray();
                    }
                    else
                    {
                        cache.Add(key, span.Slice(index, valLength).ToArray());
                    }
                    index += valLength;

                    if (index >= span.Length - 1)
                        break;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }
}
